package com.takeaway.servlet;

import com.takeaway.const_.Const;
import com.takeaway.pojo.Shop;
import com.takeaway.pojo.User;
import com.takeaway.service.serviceimpl.ComplaintServiceImpl;
import com.takeaway.service.serviceimpl.ShopServiceImpl;
import com.takeaway.service.serviceimpl.UserServiceImpl;
import com.takeaway.service.serviceinterface.ComplaintService;
import com.takeaway.service.serviceinterface.ShopService;
import com.takeaway.service.serviceinterface.UserService;
import com.takeaway.utils.AgentManage;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 后台赖骏
 */
@SuppressWarnings("unused")
@WebServlet("/manage/*")
public class ManageServlet extends BaseServlet{
    public void admitAudit(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        ShopService shopService = new ShopServiceImpl();
        AgentManage agentManage = new AgentManage(shopService);
        ShopService agent = (ShopService)agentManage.getAgent();

            List<Shop> shops = agent.checkManageShop(Const.SHOP_MANAGE_CODE);
            req.setAttribute("shops",shops);
            req.getRequestDispatcher("/jsp/CreateShopManage.jsp").forward(req,resp);
    }
    public void admitShop(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        ShopService shopService = new ShopServiceImpl();
        AgentManage agentManage = new AgentManage(shopService);
        ShopService agent = (ShopService)agentManage.getAgent();
        int shopId = Integer.parseInt(req.getParameter("shopId"));
            int update = agent.updateShopCode(shopId);
            if (update>0){
                req.getRequestDispatcher("/manage/admitAudit").forward(req,resp);
            }
    }
    public void checkComplaint(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        ComplaintService complaintService = new ComplaintServiceImpl();
        AgentManage agentManage = new AgentManage(complaintService);
        ComplaintService agent = (ComplaintService) agentManage.getAgent();
        Object[] objects;
            objects = agent.checkComplaints();
            req.setAttribute("complaints",objects[0]);
            req.setAttribute("users",objects[1]);
            req.getRequestDispatcher("/jsp/Complaints.jsp").forward(req,resp);

    }
    public void deleteComplaint(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        ComplaintService complaintService = new ComplaintServiceImpl();
        AgentManage agentManage = new AgentManage(complaintService);
        ComplaintService agent = (ComplaintService) agentManage.getAgent();
        int complaintId = Integer.parseInt(req.getParameter("complaintId"));
            int objects = agent.deleteComplaint(complaintId);
            if (objects>0){
                req.getRequestDispatcher("checkComplaint").forward(req,resp);

            }

    }
    public void checkAllUser(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        UserService userService = new UserServiceImpl();
        AgentManage agentManage = new AgentManage(userService);
        UserService agent = (UserService)agentManage.getAgent();
            List<User> users = agent.checkAllUser();
            req.setAttribute("users",users);
            req.getRequestDispatcher("/jsp/BanManage.jsp").forward(req,resp);
    }
    public void banRight(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        UserService userService = new UserServiceImpl();
        AgentManage agentManage = new AgentManage(userService);
        UserService agent = (UserService)agentManage.getAgent();
        int userId = Integer.parseInt(req.getParameter("userId"));
            int update = agent.banUser(userId);
            if (update > 0){
                req.setAttribute("msg",Const.OPERATE_SUCCESS_MSG);
                req.getRequestDispatcher("/jsp/Msg.jsp").forward(req,resp);
            }
    }
    public void giveRight(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        UserService userService = new UserServiceImpl();
        AgentManage agentManage = new AgentManage(userService);
        UserService agent = (UserService)agentManage.getAgent();
        int userId = Integer.parseInt(req.getParameter("userId"));
            int update = agent.recoverUser(userId);
            if (update > 0){
                req.setAttribute("msg",Const.OPERATE_SUCCESS_MSG);
                req.getRequestDispatcher("/jsp/Msg.jsp").forward(req,resp);
            }
    }
    public void banUserLogin(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        UserService userService = new UserServiceImpl();
        AgentManage agentManage = new AgentManage(userService);
        UserService agent = (UserService)agentManage.getAgent();
        int userId = Integer.parseInt(req.getParameter("userId"));
            int update = agent.banUserLogin(userId);
            if (update > 0){
                req.setAttribute("msg",Const.OPERATE_SUCCESS_MSG);
                req.getRequestDispatcher("/jsp/Msg.jsp").forward(req,resp);
            }

    }
    public void giveUserLogin(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        UserService userService = new UserServiceImpl();
        AgentManage agentManage = new AgentManage(userService);
        UserService agent = (UserService)agentManage.getAgent();
        int userId = Integer.parseInt(req.getParameter("userId"));
            int update = agent.giveUserLogin(userId);
            if (update > 0){
                req.setAttribute("msg",Const.OPERATE_SUCCESS_MSG);
                req.getRequestDispatcher("/jsp/Msg.jsp").forward(req,resp);
            }
    }
}
